package com.xzz.cloud.provider.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.xzz.cloud.entity.Payment;
import com.xzz.cloud.provider.dao.PaymentDao;
import com.xzz.cloud.provider.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author: hhz
 * @createDate: 2021-4-2
 */
@Service
public class PaymentServiceImpl implements PaymentService {
    @Autowired
    private PaymentDao paymentDao;

    @Override
    public int create(Payment payment) {
        return paymentDao.create(payment);
    }

    @Override
    public Payment getPaymentById(Long id) {
        return paymentDao.getPaymentById(id);
    }
    /**
     * 正常访问 的方法
     * @param id
     * @return
     */
    public String paymentInfo_ok(Integer id){
        return "线程池: "+Thread.currentThread().getName()+" paymentInfo_ok,Id "+id+"\t"+"O(∩_∩)O哈哈~";
    }

    /**
     * 会报错的方法  用于模拟触发降级
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "paymentInfo_timeoutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000")
    })
    public String paymentInfo_timeout(Integer id){
        Integer Timeout=3;
        try {
            TimeUnit.SECONDS.sleep(Timeout);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        int age=10/0;
        return "线程池: "+Thread.currentThread().getName()+" paymentInfo_timeout,Id "+id+"\t"+"O(∩_∩)O哈哈~  耗时"+Timeout+"秒钟";
    }
    public String paymentInfo_timeoutHandler(Integer id){
        return "线程池: "+Thread.currentThread().getName()+" paymentInfo_timeoutHandler,Id "+id+"\t"+"o(╥﹏╥)o 服务器出错了";
    }
}
